package com.moke.bean
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;

	/**
	 * Bean的控制器，通过载入外部XML文件进行功能模块初始化的控制，社区里主要的应用模块功能也由此控制器开启
	 * @author fraser
	 *
	 * [Embed(source="bean.xml", mimeType = "application/octet-stream")]
	 * 		private static var _beanDataClass:Class;
	 * 		private static var _beanDataXml:XML;
	 * 
	 * 		public function Test()
	 * 		{
	 * 			_beanDataXml = XML(new _beanDataClass());
	 * 			BeanManager.config(_beanDataXml) ;
	 * 		}
	 *
	 * <root>
	 * 	<Bean id="Test1" class="com.test.Test1" />
	 * 	<Bean id="Test2" class="com.test.Test2" />
	 * </root>
	 * 
	 */
	public class BeanManager
	{
		private static const ID_NODE:String="id";
		private static const CLASS_NODE:String="class";

		private static var _dataList:Array;
		private static var _beanMap:Dictionary;
		private static var _current:int;
		private static var _total:int;

		public static function config(xml:XML):void
		{
			_dataList=[];
			var xmllist:XMLList=xml.elements();
			var info:BeanNodeInfo;
			for each (var node:XML in xmllist)
			{
				info=new BeanNodeInfo();
				info.id=node.attribute(ID_NODE).toString();
				info.className=node.attribute(CLASS_NODE).toString();
				_dataList.push(info);
			}
			_total=_dataList.length;

			start();
		}

		/**
		 * 获取BEAN模块中被实例化的对象
		 * @param KEY 类名
		 * @return
		 *
		 */
		public static function getBeanInstance(name:String):IBean
		{
			return _beanMap[name];
		}

		/**
		 * Bean控制开启
		 *
		 */
		private static function start():void
		{
			_beanMap=new Dictionary(true);
			dispatchEvent(new BeanEvent(BeanEvent.OPEN, _current, _total));
			initClasses();
		}

		private static function initClasses():void
		{
			while (_dataList.length > 0)
			{
				var info:BeanNodeInfo=_dataList.shift();
				var cls:Class=getDefinitionByName(info.className) as Class;
				var instance:IBean=new cls();
				_beanMap[info.id]=instance;

				trace(instance, "实例化完成");
				dispatchEvent(new BeanEvent(BeanEvent.PROGRESS, _current, _total));
				_current++;
				instance.init();
			}

			dispatchEvent(new BeanEvent(BeanEvent.COMPLETE, _current, _total));
		}

		//--------------------------------------------------------
		// event
		//--------------------------------------------------------

		private static var _ed:EventDispatcher;

		private static function getED():EventDispatcher
		{
			if (_ed == null)
			{
				_ed=new EventDispatcher();
			}
			return _ed;
		}

		/**
		 * 事件
		 */
		public static function addEventListener(type:String, listener:Function):void
		{
			getED().addEventListener(type, listener);
		}

		public static function removeEventListener(type:String, listener:Function):void
		{
			getED().removeEventListener(type, listener);
		}

		public static function dispatchEvent(e:Event):void
		{
			if (hasEventListener(e.type))
			{
				getED().dispatchEvent(e);
			}
		}

		public static function hasEventListener(type:String):Boolean
		{
			return getED().hasEventListener(type);
		}

		public static function willTrigger(type:String):Boolean
		{
			return getED().willTrigger(type);
		}
	}
}
